package com.clt.ess.crypt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;



public class MyJMJ {
	
	public static boolean JMJ_Verify(byte[] bMsg,byte[] bSignVal){
		return true;
	}
	
	public static byte[] JMJ_Sign(byte[] bMsg){
//		PBCAgent2G agent = new PBCAgent2G();
//		int iRet = agent.getReturnCode();
//		if(iRet < 0)
//			return null;
//		String sIP = "";
//		int iPort = 0;
//		String sPwd = "";
//		String sDN = "";
//		System.out.println("ip port password  .....................");
//		agent.openSignServer(sIP,iPort,sPwd);
//		iRet = agent.getReturnCode();
//		if(iRet < 0)
//			return null;	
//		
//		
//		
//		byte[] bSignVal = null;
//		try {
//			NetSignAgent signer = new NetSignAgent();
//			NetSignResult netSignVal =  signer.rawSignature(bMsg,sDN,"SM3",false);
//	//		netSignVal.
//		} catch (NetSignAgentException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ServerProcessException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	//	FormatSM2Signature.formatSignedMsg(netSignVal.)
//		
//		agent.closeSignServer();
//		
		PrivateKey prv = null;
		try {
			File f = new File("c:/prv.dat");
			FileInputStream fis = new FileInputStream(f);
			int iLen = (int) f.length();
			byte[] bPrv = new byte[iLen];
			fis.read(bPrv);
			fis.close();
			
			PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bPrv);
			KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
			prv = keyFactory.generatePrivate(keySpec);
			byte[] bSignVal = null;
			bSignVal = GmUtil.signSm3WithSm2(bMsg,"1234567812345678".getBytes(),prv);
			System.out.println(bSignVal.length);
			return bSignVal;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GeneralSecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		byte[] signVal = MyJMJ.JMJ_Sign("123123123123".getBytes());
		
	}

}
